package cn.operation.api.rest;


import cn.operation.api.dto.CodeDTO;
import cn.operation.api.dto.OrderDTO;
import cn.operation.backend.enums.DataTypeEnum;
import cn.operation.backend.enums.MessageCodeEnum;
import cn.operation.backend.service.ItfLatestCodePondService;
import cn.operation.backend.service.OrderService;
import cn.operation.backend.util.BusinessException;
import cn.operation.backend.util.ResultDTOWithPagination;
import cn.operation.base.model.ItfLatestCodePond;
import cn.operation.base.model.OrderDetailDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/api/order", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/order",description = "订单接口")
public class OrderRest {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderRest.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private ItfLatestCodePondService itfLatestCodePondService;

    /**
     * 根据订单号，获取订单明细信息
     * @param orderCode
     * @return
     */
    @RequestMapping(value = "/getOrderDetail" ,method = RequestMethod.POST)
    @ApiOperation(value = "根据服务订单号，获取订单明细信息",notes = "提交账单的编号",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultDTOWithPagination<OrderDTO> getOrderDetail(@ApiParam(value = "订单号", required = true)@RequestParam(value = "orderCode" ,required = true) String orderCode){
        LOGGER.info("OrderRest.getOrderDetail  params ：{}",orderCode);
        ResultDTOWithPagination<OrderDTO> result = new ResultDTOWithPagination<OrderDTO>(true, null, "提交成功");
        OrderDTO orderDTO=new OrderDTO();
        try{
            OrderDetailDO orderDetailDO = orderService.queryBySSOrderCode(orderCode);
            BeanUtils.copyProperties(orderDetailDO,orderDTO);
            result.setData(orderDTO);
            result.setMessageCode("200");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("提交失败");
            result.setMessageCode("500");
            LOGGER.error("order.getOrderDetail error: {}", e);
        }
        return result;
    }

    /**
     * 查询慧运车最新被修改的订单号列表
     * @return
     * @throws Exception
     */

    @RequestMapping(value = "/getOrderCodes", method = RequestMethod.GET)
    @ApiOperation(value = "查询慧运车最新被修改的订单号列表。", notes = "查询慧运车最新被修改的订单号列表。", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultDTOWithPagination<List<CodeDTO>> getOrderCodes() throws Exception {
        LOGGER.info("OrderRest.getOrderCodes params : {}");
        ResultDTOWithPagination<List<CodeDTO>> result = new ResultDTOWithPagination<List<CodeDTO>>(true, null, "查询成功");
        try {
            List<ItfLatestCodePond> dataList = itfLatestCodePondService.queryCodes(DataTypeEnum.SERVICE_ORDER.getValue());
            List<CodeDTO> list = new ArrayList<>();
            dataList.stream().forEach(i ->{
                CodeDTO dto = new CodeDTO();
                dto.setCode(i.getCode());
                list.add(dto);
            });
            result.setData(list);
            result.setMessageCode(MessageCodeEnum.SUCCESS.getValue());
        } catch (Exception e) {
            LOGGER.error("WayBillRest.getWaybillCodes error: {}", e);
            result.setMessageCode(MessageCodeEnum.SERVER_ERROR.getValue());
            throw new BusinessException(e.getMessage());
        }
        return result;
    }
}